import os, logging
from pyhildonmemer import ui, services, commons, configs
class GenericAction(object):
    def __init__(self, servs, gui, config, post = None):
        self.post = post,
        self.ui = gui
        self.services = servs
        self.config = config

    def show(self):
        raise NotImplementedError

    def close(self):
        raise NotImplementedError

class ConfigActions(GenericAction):
    def __init__(self, servs, gui, arch = None):
        super(ConfigActions, self).__init__(servs, gui, self)

        self.setupConfig(arch)

        self.cfg = configs.Config.getInstance(arch, [configs.ConfigSqlite])
        self.cfg.init()

        servs = self.cfg.getServices()
        for service in servs:
            try:
                eval(service)
            except:
                CommonActions.log("Import service failed: %s" % service);

        self.cfgWin = None

    def close(self):
        if not(self.cfg == None):
            self.cfg.close()


    def setupConfig(self, arch):
        if arch == None:
            configDir = os.path.expanduser("~")+os.sep+".hildonmemer"
            if not os.path.exists(configDir):
                os.mkdir(configDir)
            arch = configDir + os.sep + "db.sqlite"

        if not os.path.exists(arch):
            servsList = self.services.getList()
            ins = []
            for importme in servsList:
                ins.append(importme.getHowImport())
            configs.Config.install(arch, ins, [configs.ConfigSqlite])

    def getLogins(self, servs = []):
        return self.cfg.getLogins()

    def setLogin(self, login = None, service = None, loginName = "", passwd = ""):
        if not login == None:
            self.cfg.setLogin(login)
        if not (loginName == None or service == None):
            self.cfg.setLogin(commons.Login(service, loginName, passwd))

    def getAttr(self, attrName):
        """
            Get some config
        """
        return self.cfg.getAttr(attrName)

    def setAttr(self, attrName, attrVal):
        """
            Set some config
        """
        self.cfg.setAttr(attrName, attrVal)

    def save(self):
        self.cfg.save()

    def refreshTime(self, time = None):
        if time == None:
            return self.getAttr("refreshTime")
        self.setAttr("refreshTime", time)
        return time

    def getConfiguredServices(self):
        return [login.getService() for login in self.getLogins()]

    def show(self):
        if self.cfgWin == None:
            self.cfgWin = self.ui.getConfig()
            self.cfgWin.onUpdateRefreshTime(self.refreshTime)
            self.cfgWin.updateServicesList(self.services, self.getConfiguredServices(), self.toggleService)
        self.cfgWin.show(self.hide)

    def toggleService(self, service):
        pass

    def hide(self):
        pass

class PostActions(GenericAction):
    def __init__(self, servs, gui, config, post = None):
        self.post = post
        super(PostAction, self).__init__(servs, gui, config)

    def comment(self):
        pass

    def delete(self):
        pass

    def view(self):
        pass

    def openUrl(self):
        pass

    def doPost(self):
        pass

class PostListActions(GenericAction):
    def __init__(self, servs, gui, config):
        super(PostListActions, self).__init__(servs, gui, config)

    def getNextPage(self):
        pass

    def getPrevPage(self):
        pass

    def search(self, params):
        pass

    def show(self):
        pass

    def close(self):
        pass

class ContactActions(GenericAction):
    def __init__(self, servs, gui, config):
        super(ContactActions, self).__init__(servs, gui, config)

    def getNextPage(self):
        pass

    def getPrevPage(self):
        pass

    def search(self):
        pass

    def delete(self, contact):
        pass

    def add(self, contact):
        pass

    def show(self):
        pass

    def close(self):
        pass

class ApplicationActions(GenericAction):
    def __init__(self, servs = None, gui = None, arch = None):
        if gui == None:
            gui = ui.Application("HildonMemer")
        if servs == None:
            servs = services.ServicesList

        cfg = ConfigActions(servs, gui, arch)
        super(ApplicationActions, self).__init__(servs, gui, cfg)
        self.postActions = None
        logging.info("App initialized")

    def close(self):
        pass

    def show(self):
        cfgServices = self.config.getConfiguredServices()
        if cfgServices == None:
            self.config.show()

        self.postActions = PostListActions(cfgServices, self.ui, self.config)
        self.postActions.show()
        self.ui.show(self.close)

class CommonActions(object):
    @staticmethod
    def openBrowser(url):
        pass

    @staticmethod
    def endApp(app):
        app.close()

    @staticmethod
    def initApp(servs = None, gui = None, arch = None):
        return ApplicationActions(servs, gui, arch)

    @staticmethod
    def log(msg):
        pass

if __name__ == "__main__":
    CommonActions.initApp().run()